package com.neusoft.movie.v_movies.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.CommentListAdapter;
import com.neusoft.movie.v_movies.adpater.MoviesAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.fragment.MoviesFragment;
import com.neusoft.movie.v_movies.javabean.CommentBean;
import com.neusoft.movie.v_movies.javabean.MoviesBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.MoviesModel;

import java.util.List;

public class SearchMovieActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView rv;

    private EditText search_movie;
    private ImageView button,left;
    private String m_name;

    @Override
    void initViews() {
        setLayout(R.layout.activity_search_movie);

        rv = (RecyclerView) findViewById(R.id.rv_search);
        rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        rv.addItemDecoration(decoration);

        button=(ImageView) findViewById(R.id.search_button);
        search_movie = (EditText) findViewById(R.id.search_movie);
        left=(ImageView) findViewById(R.id.left_button);
    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpacesItemDecoration(int space) {
            this.space=space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    @Override
    void initEvents() {
        //搜索
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchMovies();

            }
        });

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void searchMovies(){
        m_name=search_movie.getText().toString();
        MoviesModel moviesModel = new MoviesModel();
        moviesModel.searchMovies(m_name,this);
    }
    @Override
    void initData() {

    }


    @Override
    public void onSuccess(Object o, int flag) {
        switch(flag) {
            case Constants.SEARCHMOVIES:
                List<MoviesBean> list = (List<MoviesBean>) o;
                MoviesAdapter adapter = new MoviesAdapter(this, list);
                rv.setAdapter(adapter);
                break;
        }
    }

    @Override
    public void onFailed() {

    }
}
